package com.sky.controller.user;

import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小程序菜品管理")
@RestController("userDishController")
@RequestMapping("user/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品")
    public Result list(Long categoryId) {
        //为了提高性能 需要将小程序的查询菜品功能 添加到redis缓存区当中
        //先查询缓存区是否有菜品数据
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get("dish_" + categoryId);
        //如果查询到缓存数据 直接返回给小程序端
        if (list != null && !list.isEmpty()) {
            return Result.success(list);
        }
        //如果查询不到缓存数据 就查询mysql数据库
        list = dishService.list(categoryId);
        //将查询到的数据添加到redis缓存区中
        redisTemplate.opsForValue().set("dish_" + categoryId, list);
        return Result.success(list);
    }
}
